﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HidesaburoDotNET.Core;
using HidesaburoDotNET.Core.AI;

namespace HidesaburoMohjang
{
    public partial class MainWindow : Form
    {
        TehaiDrawer drawer;
        KawaDrawer[] kawaDrawers = new KawaDrawer[4];
        Field field;
        Player player;
        Operator human;
        List<Hai> selectedHai = new List<Hai>();
        bool selectingChiHai;
        bool selectedChihai;

        List<System.Windows.Forms.Button> buttons = new List<Button>();
        public MainWindow()
        {
            InitializeComponent();
            InitField();
            buttons.Add(button_Through);
            buttons.Add(button_Tumo);
            buttons.Add(button_Richi);
            buttons.Add(button_Chi);
            buttons.Add(button_Pon);
            buttons.Add(button_Kan);
        }

        public void InitField()
        {
            player = new Player();
            human = new Operator(this);
            player.AI = human;

            FieldBuilder builder = new FieldBuilder();
            builder.AddPlayer(player);

            field = builder.CreateField();


            drawer = new TehaiDrawer(player.Tehai);
            drawer.Position = new Point(10, 180);

            for (int i = 0; i < 4; i++)
            {
                kawaDrawers[i] = new KawaDrawer(field.Players[i].Kawa);
                kawaDrawers[i].Position = new Point(10, 40 * i);
            }

            human.DiscardEvent += new OperatorEvent<Hai>(human_DiscardEvent);
            human.CheckActionEvent += new ActionEvent(human_CheckActionEvent);
            player.Activated += new EventHandler(player_Activated);

            button_Chi.Click += new EventHandler(DoActionButtonChi);
            button_Through.Click += new EventHandler(DoActionButtonThrough);
            button_Pon.Click += new EventHandler(DoActionButtonPon);
            button_Kan.Click += new EventHandler(DoActionButtonKan);
            button_Tumo.Click += new EventHandler(DoAvtionButtonTsumo);
            button_Richi.Click += new EventHandler(DoAvtionButtonRichi);


        }

        /// <summary>
        /// プレイヤーのターンが回ってくる毎に呼び出される。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void player_Activated(object sender, EventArgs e)
        {
            foreach (Button b in buttons)
            {
                b.Enabled = false;
            }
        }
        ActionEventArgs actionArgs;
        /// <summary>
        /// 鳴ける牌があった場合に呼び出される
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void human_CheckActionEvent(Operator sender, ActionEventArgs args)
        {
            selectedHai = new List<Hai>();
            foreach (Button b in buttons)
            {
                b.Enabled = false;
            }
            foreach (NakiAction n in args.AvailableActions)
            {
                UpdateButtons(n);
            }
            
            button_Through.Enabled = true;

            actionArgs = args;




            
            Refresh();
        }

        private void DoAction(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button1.Click -= new EventHandler(DoAction);
            actionArgs.WaitValue = actionArgs.AvailableActions[0];
            
        }

        private void DoAvtionButtonTsumo(object sender,EventArgs e)
        {

        }
        private void DoAvtionButtonRichi(object sender, EventArgs e)
        {

        }

        private void DoActionButtonChi(object sender, EventArgs e)
        {

            button_Chi.Enabled = false;
            if (button_Chi.Text == "チー")
            {
                foreach (Button b in buttons)
                {
                    b.Enabled = false;
                }
                StartChiSelect();
            }
            //else if (button_Chi.Text == field.DiscardedHai.MakeMentsuFromNakiAction(NakiAction.Chi_Low).ToString())
            else if (
                player.Tehai.CanChi(
                field.ActivePlayer.Kawa.LastDiscard, NakiAction.Chi_Low))
            {
                actionArgs.WaitValue = NakiAction.Chi_Low;
                EndChiSelect();
            }
        }

        private void DoActionButtonThrough(object sender, EventArgs e)
        {
            button_Through.Enabled = false;
            actionArgs.WaitValue = NakiAction.None;
        }

        private void DoActionButtonPon(object sender, EventArgs e)
        {
            button_Pon.Enabled = false;
            if (button_Pon.Text == "ポン")
            {
                actionArgs.WaitValue = NakiAction.Pon;
            }
            //if (button_Pon.Text == field.DiscardedHai.MakeMentsuFromNakiAction(NakiAction.Chi_Mid).ToString())
            else if (
                player.Tehai.CanChi(
                field.ActivePlayer.Kawa.LastDiscard, NakiAction.Chi_Mid))
            {
                actionArgs.WaitValue = NakiAction.Chi_Mid;
                EndChiSelect();
            }
        }

        private void DoActionButtonKan(object sender,EventArgs e)
        {
            button_Kan.Enabled = false;
            if (button_Kan.Text == "カン")
            {
                actionArgs.WaitValue = NakiAction.Kan;
            }
            //if (button_Kan.Text == field.DiscardedHai.MakeMentsuFromNakiAction(NakiAction.Chi_High).ToString())
            else if (
                player.Tehai.CanChi(
                field.ActivePlayer.Kawa.LastDiscard, NakiAction.Chi_High))
            {
                actionArgs.WaitValue = NakiAction.Chi_High;
                EndChiSelect();
            }
        }

        private void StartChiSelect()
        {
            int count = 0;
            if (actionArgs.AvailableActions.Contains(NakiAction.Chi_High))
            {
                count++;
                button_Kan.Enabled = true;
                button_Kan.Text = field.DiscardedHai.MakeMentsuFromNakiAction(NakiAction.Chi_High).ToString();
            }
            if (actionArgs.AvailableActions.Contains(NakiAction.Chi_Mid))
            {
                count++;
                button_Pon.Enabled = true;
                button_Pon.Text = field.DiscardedHai.MakeMentsuFromNakiAction(NakiAction.Chi_Mid).ToString();
            }
            if (actionArgs.AvailableActions.Contains(NakiAction.Chi_Low))
            {
                count++;
                button_Chi.Enabled = true;
                button_Chi.Text = field.DiscardedHai.MakeMentsuFromNakiAction(NakiAction.Chi_Low).ToString();
            }
            if (count == 1)
            {
                EndChiSelect();
                if (actionArgs.AvailableActions.Contains(NakiAction.Chi_High))
                {
                    actionArgs.WaitValue = NakiAction.Chi_High;
                }
                if (actionArgs.AvailableActions.Contains(NakiAction.Chi_Mid))
                {
                    actionArgs.WaitValue = NakiAction.Chi_Mid;
                }
                if (actionArgs.AvailableActions.Contains(NakiAction.Chi_Low))
                {
                    actionArgs.WaitValue = NakiAction.Chi_Low;
                }
            }
        }
        private void EndChiSelect()
        {
            button_Chi.Text = "チー";
            button_Kan.Text = "カン";
            button_Pon.Text = "ポン";
            button_Chi.Enabled = false;
            button_Pon.Enabled = false;
            button_Kan.Enabled = false;
        }

        private void UpdateButtons(NakiAction n)
        {
            Button b = null;
            
            switch (n)
            {
                case NakiAction.Pon:
                    b = button_Pon;
                    
                    break;
                case NakiAction.Kan:
                    b = button_Kan;
                    break;
                case NakiAction.Ron:
                    b = button_Tumo;
                    break;
            }
            if (b != null)
            {
                b.Enabled = true;
                return;
            }
            if ((int)n / 8 == 1)
            {
                button_Chi.Enabled = true;
                return;
            }
        }

        OperatorEventArgs<Hai> discardArgs;
        void human_DiscardEvent(Operator sender, OperatorEventArgs<Hai> e)
        {
            discardArgs = e;
            Refresh();

        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button1.Click -= new EventHandler(button1_Click);
            field.StartGame();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            drawer.Draw(e.Graphics);
            foreach (KawaDrawer kd in kawaDrawers)
            {
                kd.Draw(e.Graphics);
            }
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (player.IsActive)
            {
                Hai h = drawer.GetIntersectedHai(e.Location);
                if (h != null)
                {
                    discardArgs.WaitValue = h;
                    this.Refresh();
                }
            }
        }

        bool CheckSelectChi()
        {

            return false;
        }



        private void MainWindow_Load(object sender, EventArgs e)
        {

        }




    }
}
